#include <iostream>	
#include <vector>
#include <algorithm>
#include <string>
using namespace std;

//struct to hold Team data
struct Team{
	string name;
	int points, gScored, gAllowed, wins, ties, losses, games;
	Team() : points(0), gScored(0), gAllowed(0), wins(0), ties(0), losses(0), games(0) {}
};
//output
istream& operator>>(istream& is, Team& te){
	if(cin.peek() == '\n')
		cin.get();
	getline(is, te.name);
	if(cin.peek() == '\n')
		cin.get();
	return is;
}
ostream& operator<<(ostream& os, const Team& te){
	//1) Brazil 6p, 3g (2-0-1), 3gd (6-3)
	os << te.name << " "; //name
	os << te.points << "p, ";
	os << te.games << "g ";
	os << "(" << te.wins << "-" << te.ties << "-" << te.losses << "), ";
	os << (te.gScored-te.gAllowed) << "gd (" << te.gScored << "-" << te.gAllowed << ")";
	return os;
}

//struct to hold match data
struct Game{
	string teamA, teamB;
	int scoreA, scoreB;
};
//input
istream& operator>>(istream& is, Game& g){
	string str;
	string num;

	if(is.peek() == '\n')
		cin.get();

	getline(is, str, '#');//read teamA
	g.teamA = str;
	getline(is, num, '@');//read scoreA
	g.scoreA = atoi(num.c_str());
	getline(is, num, '#');//read scoreB
	g.scoreB = atoi(num.c_str());
	getline(is, str);//read teamb
	g.teamB = str;


	return is;
}

//functor for ranking
struct uvaRank{
	bool operator()(const Team& a, const Team& b){
		if(a.points != b.points)
			return a.points>b.points;
		else if(a.wins != b.wins)
			return a.wins>b.wins;
		else if((a.gScored-a.gAllowed) != (b.gScored>b.gAllowed))
			return (a.gScored-a.gAllowed) > (b.gScored>b.gAllowed);
		else if(a.gScored != b.gScored)
			return a.gScored > b.gScored;
		else if(a.games != b.games)
			return a.games < b.games;	//I THINK this is what they meant;
		else if(a.name != b.name){
			string aCpy = a.name;		//will be overwritten with an ALL CAPS version
			transform(a.name.begin(), a.name.end(), aCpy.begin(), ::tolower);
			string bCpy = b.name;
			transform(b.name.begin(), b.name.end(), bCpy.begin(), ::tolower);
			return aCpy < bCpy;
		}
		else
			return true;			//I hope this never happens
	}
};
//tourn class for stuff
class Tournament{
	string name;
	vector<Team> players;
	vector<Game> matches;
	
	size_t findTeam(string tn){
		for(size_t i = 0; i < players.size(); ++i){
			if(players[i].name == tn)
				return i;
		}
		return players.size();
	}
public:
	void process(){
		//turn matches into teams
		for(Game g : matches){

			Team* tA = &players[findTeam(g.teamA)];
			Team* tB = &players[findTeam(g.teamB)];
			
			//cout << *tA << endl;
			//cout << *tB << endl;
			//win-lose-tie
			if(g.scoreA > g.scoreB){
				tA->wins++;
				tA->points+=3;
				tB->losses++;
			}
			else if(g.scoreA < g.scoreB){
				tA->losses++;
				tB->wins++;
				tB->points+=3;
			}
			else{
				tA->ties++;
				tA->points+=1;
				tB->ties++;
				tB->points+=1;
			}

			//goals
			tA->gScored+=g.scoreA;
			tA->gAllowed+=g.scoreB;
			tB->gScored+=g.scoreB;
			tB->gAllowed+=g.scoreA;

			//games
			tA->games++;
			tB->games++;

			//cout << *tA << endl;
			//cout << *tB << endl << endl;

		}
		//sort the teams
		stable_sort(players.begin(), players.end(), uvaRank());
	}
	friend ostream& operator<<(ostream& os, const Tournament& t);
	friend istream& operator>>(istream& is, Tournament& t);
};

ostream& operator<<(ostream& os, const Tournament& t){
	os << t.name << endl;
	for(size_t i = 0; i < t.players.size(); ++i)
		cout << i+1 << ") " << t.players[i] << endl;
	return os;
}

istream& operator>>(istream& is, Tournament& t){
	if(cin.peek() == '\n')	//flush trailing newlines left by >>
		cin.get();
	getline(is, t.name);
	int teams;
	is >> teams;
	for(int j = 0; j < teams; ++j){
		Team te;
		is >> te;
		t.players.push_back(te);
	}
	int games;
	is >> games;
	for(int j = 0; j < games; ++j){
		Game g;
		is >> g;
		t.matches.push_back(g);
	}
	return is;
}

int main(){
	int T;
	cin >> T;
	for(int i = 0; i < T; ++i){
		Tournament tourny;
		cin >> tourny;
		tourny.process();
		cout << tourny;
		if(i < T-1)
			cout << endl;
	}

}
